package com.itxx.keyCase.Demo6;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.time.LocalDateTime;
import java.util.UUID;

public class TcpServer {
    public static void main(String[] args) throws IOException {
        //1.创建ServerSocket对象
        ServerSocket ss = new ServerSocket(54433);
        while (true) {
            //2.创建io流接受数据
            Socket accept = ss.accept();

            BufferedInputStream bis = new BufferedInputStream(accept.getInputStream());

            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\develop\\workspace\\Advance_Code\\day15\\src\\com\\itxx\\keyCase\\server\\" + UUID.randomUUID().toString().replace("-", "") + ".jpg"));

            byte[] bytes = new byte[1024];
            int len;
            while ((len = bis.read(bytes)) != -1) {
                bos.write(bytes, 0, len);
            }
            //4.接受完成 提醒客户端
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
            bw.write("在" + LocalDateTime.now() + "上传完成");
            bw.newLine();
            bw.flush();

            //3.释放资源
            bos.close();
            accept.close();
        }


    }
}
